package com.movie.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.movie.beans.MovieBeans;
import com.movie.beans.ReviewBeans;

public class ReviewDao {
	private Connection road;
	private ResultSet result;
	private PreparedStatement car;
	private DataSource ds;
		
	public ReviewDao(){
		try{		
			Context init = new InitialContext();
			ds = (DataSource)init.lookup("java:comp/env/SJHDB");
		}catch(Exception e){
			e.printStackTrace();
			return;		
		}
	}
	public ArrayList<ReviewBeans> viewReview(String id){
		ArrayList<ReviewBeans> list= new ArrayList<ReviewBeans>();
		
		
		String sql = "SELECT R.ID,M.MVNAME,R.RDATE,R.MDATE,R.SPOINT,M.IMAGE,T.TNAME,S.SPEECH,R.REPORT FROM REVIEW R INNER JOIN MOVIES M ON R.MCODE=M.MCODE INNER JOIN THEATER T ON R.TID=T.TID INNER JOIN SPEECH S ON R.MCODE=S.MCODE AND R.SCODE=S.SCODE WHERE ID=?";
		try{
			road = ds.getConnection();
			car = road.prepareStatement(sql);
			car.setString(1, id);			
			result = car.executeQuery();
			while(result.next()){
				if(result.getNString(1) != null){ 
					ReviewBeans rb=new ReviewBeans();	
					rb.setId(result.getNString(1));
					rb.setMvname(result.getNString(2));
					rb.setRdate(result.getNString(3));
					rb.setMdate(result.getNString(4));
					rb.setSpoint(result.getNString(5));
					rb.setImage(result.getNString(6));
					rb.setTname(result.getNString(7));
					rb.setSpeech(result.getNString(8));
					rb.setReport(result.getNString(9));
					list.add(rb);
				}
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(result != null){ try{ result.close();}catch(SQLException e){}	}
			if(car != null){ try{ car.close();}catch(SQLException e){}	}
			if(road != null){ try{ road.close();}catch(SQLException e){}	}
		}
		return list;
	}
	
}
